package com.cskaoyan.config.aliyun;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cskaoyan.bean.BaseRespVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;

/**
 * @author wangdezhao
 * @date 2021/6/9
 * @description
 */
@Data
@ConfigurationProperties(prefix = "aliyun")
@Component
public class AliyunComponent {
    private String accessKeyId;
    private String accessKeySecret;
    private Oss oss ;
    private Sms sms ;


    public OSSClient getOssClient(){
        OSSClient ossClient = new OSSClient(oss.getEndPoint(), accessKeyId, accessKeySecret);
        return ossClient;

    }
    public PutObjectResult fileUpload(String fileName, File file){
        OSSClient ossClient = getOssClient();
        PutObjectResult putObjectResult = ossClient.putObject(oss.getBucket(), fileName, file);
        return putObjectResult;
    }
    public PutObjectResult fileUpload(String fileName, InputStream inputStream){
        OSSClient ossClient = getOssClient();
        PutObjectResult putObjectResult = ossClient.putObject(oss.getBucket(), fileName, inputStream);
        return putObjectResult;
    }

    /**
     * 发送短信；aliyun的code格式要求 [a-zA-Z0-9]
     * @param phoneNumber
     * @param code
     * @return null->失败
     */
    public CommonResponse sendMsg(String phoneNumber,String code){

        String signName = sms.getSignName();
        String templateCode = sms.getTemplateCode();

        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\": \""+ code +"\"}");
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response;
    }

}
